package com.trkj.service.hjj;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.sql.rowset.serial.SerialException;

import org.hibernate.annotations.NaturalId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.trkj.dao.hjj.AffaircategoryDAO;
import com.trkj.dao.hjj.AttendanceruleDAO;
import com.trkj.dao.hjj.AttendanceruleDAO;
import com.trkj.dao.hjj.HolidayDAO;
import com.trkj.dao.hjj.ShiftDAO;
import com.trkj.dao.hjj.TimecardDAO;
import com.trkj.mybatisdao.StaffinformationDAOI;
import com.trkj.mybatisdao.TimesCardDAO;
import com.trkj.pojo.Affaircategory;
import com.trkj.pojo.Attendancerule;
import com.trkj.pojo.Holiday;
import com.trkj.pojo.Shift;
import com.trkj.pojo.StaffInformation;
import com.trkj.pojo.Timecard;
import com.trkj.service.ServiceException;
import com.trkj.vo.TimecardVo;

@Service
@Transactional(rollbackFor=Exception.class)//事物回滚
public class DataDatumService {
	@Autowired//假种dao
	private HolidayDAO holidayDAO;
	@Autowired//规则结果dao
	private AttendanceruleDAO attendanceruledao;
	@Autowired//事务类别dao
	private AffaircategoryDAO affaircategorydao;
	@Autowired//班次dao
	private ShiftDAO shiftdao;
	@Autowired//考勤卡dao
	private TimecardDAO timecarddao;
	@Autowired
	private TimesCardDAO timesCardDAO;
	@Autowired
	private TimecardDAO timeCardDAO;//考勤卡
	@Autowired
	private StaffinformationDAOI staffinformationDAOI;
	/**
	 * 新增考勤卡
	 */
	public boolean inserTimeCard(String stainName)throws ServiceException{
		try {
			Timecard ts = new Timecard();
			ts.setTimecardDate(new Timestamp(new Date().getTime()));
			ts.setTimecardBeizhu("无");
			ts.setTimecardZt(new BigDecimal(0));
			ts.setTimecardPerson(stainName);
			timeCardDAO.save(ts);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载考勤卡
	 * @param timecardId
	 * @return
	 */
	public Timecard loadTimecard(Long timecardId){
		return timeCardDAO.findOne(timecardId);
	}
	/**
	 * 修改考勤卡状态
	 * @param 
	 * @param 
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateTimecard_state(Long timecardId,Long timecardZt)throws ServiceException{
		
			/*List<Object[]> list = timeCardDAO.findByTandS(timecardId);
			List<TimecardVo> listvo = new ArrayList<>();
			if(list!=null){
				for (Object[] obj : list) {
					TimecardVo tvo = new TimecardVo();
					
					Timecard t = (Timecard) obj[0];
					StaffInformation s = (StaffInformation) obj[1];
					
					
					
				}
			}*/
			Timecard tt = timeCardDAO.findOne(timecardId);
			if(tt.getTimecardBeizhu()=="无"||tt.getTimecardBeizhu().equals("无")){
				tt.setTimecardZt(new BigDecimal(timecardZt));
				return true;
			}else if(tt.getTimecardBeizhu()=="有"||tt.getTimecardBeizhu().equals("有")){
				return false;
			}
			return true;
		
	}
	
	/**
	 * 删除考勤卡
	 * @param
	 * @param 
	 * @return
	 * @throws ServiceException
	 */
	public boolean delTimecard_state(Long timecardId)throws ServiceException{
		
			Timecard tt = timeCardDAO.findOne(timecardId);
			if(tt.getTimecardBeizhu()=="无"||tt.getTimecardBeizhu().equals("无")){
				timeCardDAO.delete(tt);
				return true;
			}else if(tt.getTimecardBeizhu()=="有"||tt.getTimecardBeizhu().equals("有")){
				return false;
			}
			return true;
		
	}
	/**
	 * 查询所有的考勤卡
	 * @return
	 */
	public JSONArray findAllTimeCard(){
		return timesCardDAO.findAllTimeCard();
	}
	/**
	 * 根据编号查询考勤卡
	 * @param timecardId
	 * @return
	 */
	public JSONArray findTimeCardById(Integer timecardId){
		return timesCardDAO.findTimeCardById(timecardId);
	}
	
	
	/**
	 * 查询所有所有规则结果
	 * @return
	 * @throws ServiceException
	 */
	/*public List<Attendancerule> findAllAttendancerule()throws ServiceException{
		List<Attendancerule> list;
		try {
			list = (List<Attendancerule>) attendanceruledao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);//抛出异常
		}
		return list;
	}*/
	/**
	 * 分页查询班次
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Shift> pageShift(int page,int rows){
		return shiftdao.pageFindAllShift(new PageRequest(page-1, rows));
	}
	/**
	 * 总记录数班次
	 * @return
	 */
	public long pageByCountShift(){
		return shiftdao.count();
	}
	/**
	 * 分页查询 班次
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Shift> pageShiftByName(String shiftName,int page,int rows)throws ServiceException{
		try {
			if(shiftName==null||"".equals(shiftName)){
				return shiftdao.pageFindAllShift(new PageRequest(page-1, rows));
			}else{
				return shiftdao.pageShiftByName("%"+shiftName+"%",new PageRequest(page-1, rows));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 班次添加
	 * @param 
	 * @param 
	 * @param 
	 * 
	 * 
	 * @return
	 * @throws ServiceException
	 */
	public boolean AddShift(Shift shift,String stainName)throws ServiceException{
		
		try {
			Shift s = new Shift();
			s.setShiftName(shift.getShiftName());
			s.setShiftFounddate(new Timestamp(new Date().getTime()));
			s.setShiftRemarks(shift.getShiftRemarks());
			s.setShiftState(shift.getShiftState());
			s.setShiftCategory(shift.getShiftCategory());
			s.setShiftPerson(stainName);
			shiftdao.save(s);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 删除班次
	 * @param
	 * @return
	 * @throws ServiceException
	 */
	public boolean delShift(Long shiftId)throws ServiceException{
		try {
			Shift shift  = shiftdao.findOne(shiftId);
			shiftdao.delete(shift);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 修改班次状态
	 * @param 
	 * @param 
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateShift_state(Long shiftId,Long shiftState)throws ServiceException{
		try {
			Shift shift = shiftdao.findOne(shiftId);
			shift.setShiftState(new BigDecimal(shiftState));
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载班次
	 * @param 
	 * @return
	 */
	public Shift LoadShift(Long shiftId){
		return shiftdao.findOne(shiftId);
	}
	/**
	 * 修改班次
	 * @param 
	 * @param 
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateShift(Long shiftId,Shift s)throws ServiceException{
		try {
			Shift shift = shiftdao.findOne(shiftId);
			shift.setShiftCategory(s.getShiftCategory());
			shift.setShiftUpdate(new Timestamp(new Date().getTime()));
			shift.setShiftName(s.getShiftName());
			shift.setShiftRemarks(s.getShiftRemarks());
			shift.setShiftState(s.getShiftState());
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	/**
	 * 规则结果添加
	 * @param rule_name
	 * @param isRes
	 * @param thems
	 * @param marks
	 * @param isState
	 * @return
	 * @throws ServiceException
	 */
	public boolean AddAttendancerule(String rule_name,String isRes,
		String thems,String marks,Integer isState,String stainName)throws ServiceException{
		
		try {
			Attendancerule attr = new Attendancerule();
			attr.setAttruName(rule_name);
			attr.setAttruRepeat(isRes);
			attr.setAttruAffair(thems);
			attr.setAttruRemarks(marks);
			attr.setAttruPerson(stainName);
			attr.setAttruState(new BigDecimal(isState));
			attr.setAttruFounddate(new Timestamp(new Date().getTime()));
			attendanceruledao.save(attr);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 规则结果修改
	 * @param attruId
	 * @param attends
	 * @return
	 * @throws ServiceException
	 */
	public boolean UpdateAttendancerule(Long attruId,Attendancerule attends)throws ServiceException{
		try {
			Attendancerule attend = attendanceruledao.findOne(attruId);//通过id找到
			attend.setAttruAffair(attends.getAttruAffair());
			attend.setAttruUpdate(new Timestamp(new Date().getTime()));
			attend.setAttruEndupdate(new Timestamp(new Date().getTime()));
			attend.setAttruName(attends.getAttruName());
			attend.setAttruRemarks(attends.getAttruRemarks());
			attend.setAttruState(attends.getAttruState());
			attend.setAttruRepeat(attends.getAttruRepeat());
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载
	 * @param attid
	 * @return
	 */
	public Attendancerule LoadAttendancerule(Long attid){
		return attendanceruledao.findOne(attid);
	}
	/**
	 * 修改规则结果状态
	 * @param attid
	 * @param state
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateAttendancerule_state(Long attid,Long state)throws ServiceException{
		try {
			Attendancerule attru = attendanceruledao.findOne(attid);
			attru.setAttruState(new BigDecimal(state));
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 根据编号删除规则结果
	 * @param attid
	 * @return
	 * @throws ServiceException
	 */
	public boolean deleteAttendancerule_rule_(Long attid)throws ServiceException{
		try {
			Attendancerule attru = attendanceruledao.findOne(attid);
			attendanceruledao.delete(attru);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 分页查询
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Attendancerule> pageAttendancerule(int page,int rows){
		return attendanceruledao.findPageAttendancerule(new PageRequest(page-1, rows));
	}
	/**
	 * 总记录数
	 * @return
	 */
	public long pageByCount(){
		return attendanceruledao.count();
	}
	
	
	/**
	 * 分页查询假种
	 * @param page
	 * @param rows
	 * @return
	 */
	/*public List<Holiday> pageHoliday(int page,int rows){
		return holidayDAO.PageHolidays(new PageRequest(page-1, rows));
	}*/
	public JSONArray pageHoliday(){
		return staffinformationDAOI.findAllwu();
		
	}
	/**
	 * 分页查询 假种 模糊搜索
	 * @param page
	 * @param rows
	 * @return
	 */
	public JSONArray pageHolidayByName(String holidayName)throws ServiceException{
		return staffinformationDAOI.findAllByName("%"+holidayName+"%");
		
	}
	
	/**
	 * 新增假种
	 * @param aff
	 * @return
	 * @throws ServiceException
	 */
	public boolean addHoliday(Holiday aff,String stainName)throws ServiceException{
		try {
			Holiday holiday = new Holiday();
			holiday.setHolidayName(aff.getHolidayName());
			holiday.setHolidayFounddate(new Timestamp(new Date().getTime()));
			holiday.setHolidaySalary(aff.getHolidaySalary());
			holiday.setHolidayPersonal(aff.getHolidayPersonal());
			holiday.setHolidayLegal(aff.getHolidayLegal());
			holiday.setHolidayRemarks(aff.getHolidayRemarks());
			holiday.setHolidayPerson(stainName);
			holiday.setHolidayType(aff.getHolidayType());
			holidayDAO.save(holiday);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除假种
	 * @param affcaId
	 * @return
	 * @throws ServiceException
	 */
	public boolean delHoliday(Long holidayId)throws ServiceException{
		try {
			Holiday holiday  = holidayDAO.findOne(holidayId);
			holidayDAO.delete(holiday);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载假种
	 */
	public Holiday loadHoliday(Long holidayId){
		return holidayDAO.findOne(holidayId);
	}
	/**
	 * 修改假种
	 * @param affcaId
	 * @param aff
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateHoliday(Long holidayId,Holiday aff)throws ServiceException{
		try {
			Holiday ho = holidayDAO.findOne(holidayId);
			ho.setHolidayName(aff.getHolidayName());
			ho.setHolidayType(aff.getHolidayType());
			ho.setHolidayRemarks(aff.getHolidayRemarks());
			ho.setHolidayLegal(aff.getHolidayLegal());
			ho.setHolidaySalary(aff.getHolidaySalary());
			ho.setHolidayUpdate(new Timestamp(new Date().getTime()));
			ho.setHolidayPersonal(aff.getHolidayPersonal());
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	/**
	 * 修改状态 事务类型
	 * @param affcaId
	 * @param state
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateHolidayState(Long holidayId,Long state)throws ServiceException{
		try {
			Holiday aff = holidayDAO.findOne(holidayId);
			aff.setHolidayType(new BigDecimal(state));
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	/**
	 * 分页查询 事物类型
	 * @param page
	 * @param rows
	 * @return
	 */
	public JSONArray pageAffaircategory(){
		return staffinformationDAOI.findsstates();
	}
	/**
	 * 总记录数事物类型
	 * @return
	 */
	public long pageByCountAffaircategory(){
		return affaircategorydao.count();
	}
	/**
	 * 分页查询 事物类型
	 * @param page
	 * @param rows
	 * @return
	 */
	public JSONArray pageAffaircategoryByName(String affcaName)throws ServiceException{
		return staffinformationDAOI.findsstatesByBName("%"+affcaName+"%");
	}
	/**
	 * 加载事务类别
	 */
	public Affaircategory loadAffaircategory(Long affcaId){
		return affaircategorydao.findOne(affcaId);
	}
	/**
	 * 修改事务类别
	 * @param affcaId
	 * @param aff
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateAffaircategory(Long affcaId,Affaircategory aff,String stainName)throws ServiceException{
		try {
			Affaircategory affa = affaircategorydao.findOne(affcaId);
			affa.setAffcaName(aff.getAffcaName());
			affa.setAffcaState(aff.getAffcaState());
			affa.setAffcaType(aff.getAffcaType());
			affa.setAffcaOvertime(aff.getAffcaOvertime());
			affa.setAffcaUpdate(new Timestamp(new Date().getTime()));
			affa.setAffcaPerson(stainName);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}

	/**
	 * 新增事务类型
	 * @param aff
	 * @return
	 * @throws ServiceException
	 */
	public boolean addAffaircategory(Affaircategory aff,String stainName)throws ServiceException{
		try {
			Affaircategory affs = new Affaircategory();
			affs.setAffcaName(aff.getAffcaName());
			affs.setAffcaType(aff.getAffcaType());
			affs.setAffcaOvertime(aff.getAffcaOvertime());
			affs.setAffcaFounddate(new Timestamp(new Date().getTime()));
			affs.setAffcaState(aff.getAffcaState());
			affs.setAffcaPerson(stainName);
			affaircategorydao.save(affs);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 修改状态 事务类型
	 * @param affcaId
	 * @param state
	 * @return
	 * @throws ServiceException
	 */
	public boolean updateAffaircategoryState(Long affcaId,Long state)throws ServiceException{
		try {
			Affaircategory aff = affaircategorydao.findOne(affcaId);
			aff.setAffcaState(new BigDecimal(state));
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	/**
	 * 删除事务类型
	 * @param affcaId
	 * @return
	 * @throws ServiceException
	 */
	public boolean delAffaircategory(Long affcaId)throws ServiceException{
		try {
			Affaircategory aff  = affaircategorydao.findOne(affcaId);
			affaircategorydao.delete(aff);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 分页查询 搜索 规则结果
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Attendancerule> pageAttendanceruleByName(String attruName,int page,int rows)throws ServiceException{
		try {
			if(attruName==null||"".equals(attruName)){
				return attendanceruledao.findPageAttendancerule(new PageRequest(page-1, rows));
			}else{
				return attendanceruledao.findPageAttendByName("%"+attruName+"%",new PageRequest(page-1, rows));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 查询所有假种
	 * @return
	 * @throws ServiceException
	 */
	public List<Holiday> findAllHoliday()throws ServiceException{
		List<Holiday> list;
		try {
			list = (List<Holiday>) holidayDAO.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);//抛出异常
		}
		return list;
	}
	
	/**
	 * 查询所有的班次
	 * @return
	 * @throws ServiceException
	 */
	public List<Shift> findAllShift()throws ServiceException{
		List<Shift> list;
		try {
			list = (List<Shift>) shiftdao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
		return list;
	}
	/**
	 * 查询所有的考勤卡
	 * @return
	 * @throws ServiceException
	 */
	public List<Timecard> findAllTimecard()throws ServiceException{
		List<Timecard> list;
		try {
			list = (List<Timecard>) timecarddao.findAll();
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
		return list;
	}
}
